﻿using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using AllIsWell.Core.Models;
using AllIsWell.Persistance.Ef;

namespace AllIsWell.Web
{
    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("CreatePage", "Page/CreatePage/{destinyId}", 
                                new { controller = "Page", action = "CreatePage" }, 
                                new { destinyId = @"\d+" });
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        // ReSharper disable InconsistentNaming
        protected void Application_Start()
        // ReSharper restore InconsistentNaming
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            SetDataBaseInitializer();
            SetUpDependencyInjection();
            SetUpModelBinders();
        }

        private void SetDataBaseInitializer()
        {
            Database.SetInitializer(new MyDiaryDbInitStratergy());
        }

        private void SetUpDependencyInjection()
        {
            //ControllerBuilder.Current.SetControllerFactory(new NinjectDependencyResolver());
            DependencyResolver.SetResolver(new NinjectDependencyResolver());
        }

        private void SetUpModelBinders()
        {
            ModelBinders.Binders.Add(typeof(Page), new PageModelBinder());
        }
    }
}